package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"log"
	"modbus-go-demo/global"
	"modbus-go-demo/middleware"
	"modbus-go-demo/routers"
)

func main() {

	appMode := global.AppConfig.Section("").Key("app_mode").String()
	appPort := global.AppConfig.Section("").Key("http_port").String()

	var r *gin.Engine
	if appMode == "production" {
		gin.SetMode(gin.ReleaseMode)
		// 设置全局日志中间件
		r = gin.New()
		r.Use(middleware.LoggerToFile())
		r.Use(middleware.CustomRecovery())
	} else {
		r = gin.Default()
	}
	routers.GinConfigure(r)

	r.GET("/", func(context *gin.Context) {
		context.String(200, "server is running on "+appPort)
	})

	routers.ApiRouterInit(r)

	fmt.Printf("%-32s%-32s\n", "http listen on "+appPort, "OK")
	err := r.Run(":" + appPort)
	if err != nil {
		log.Fatal(err.Error())
	}
}
